package cn.hrlly.tao2shou.callback;

import com.google.gson.Gson;
import com.zhy.http.okhttp.callback.Callback;

import cn.hrlly.tao2shou.R;
import cn.hrlly.tao2shou.domain.Status;
import cn.hrlly.tao2shou.listener.OnFindPasswordListener;
import cn.hrlly.tao2shou.listener.OnRegisterListener;
import okhttp3.Request;
import okhttp3.Response;

/**
 * User: gengliang
 * Date: 2016-01-22
 * Time: 09:53
 */

public class GetCodeCallback extends Callback<Status> {

    private OnRegisterListener onRegisterListener;
    private OnFindPasswordListener onFindPasswordListener;

    public GetCodeCallback(OnRegisterListener onRegisterListener) {
        this.onRegisterListener = onRegisterListener;
    }

    public GetCodeCallback(OnFindPasswordListener onFindPasswordListener) {
        this.onFindPasswordListener = onFindPasswordListener;
    }

    @Override
    public Status parseNetworkResponse(Response response) throws Exception {
        String string = response.body().string();
        Status status = new Gson().fromJson(string, Status.class);
        return status;
    }

    @Override
    public void onError(Request request, Exception e) {
        if (onRegisterListener != null) {
            onRegisterListener.getVerificationCodeFail(R.string.network_connection_error);
        }
        else{
            onFindPasswordListener.getVerificationFail(R.string.network_connection_error);
        }
    }

    @Override
    public void onResponse(Status status) {
        if (status.getStatus() == 1) {
            if(onRegisterListener!=null) {
                onRegisterListener.getVerificationCodeFail(status.getErrorMessage());
            }
            else{
                onFindPasswordListener.getVerificationFail(status.getErrorMessage());
            }
        }
    }
}
